package GUITest;

/*
 * @(#)TestJToggleButton.java
 *
 * Summary: demonstrate use of javax.swing.ButtonGroup with javax.swing.JToggleButton.
 *
 * Copyright: (c) 2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          see http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2009-01-01 - initial version
 */

// Using a ButtonGroup with JToggleButton.
// This code lets you select one of three types
// of flower with three toggle buttons.
// The ButtonGroup itself is not visible.
// It does not draw a line around the toggle buttons, for example.
// It just ensures only one option is selected
// at a elapsedTime.
// It is up to you to layout the toggle buttons in some
// logical pattern that indicates they belong together.

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * demonstrate use of javax.swing.ButtonGroup with javax.swing.JToggleButton.
 *
 * @author Roedy Green, Canadian Mind Products
 *
 * @version 1.0 2009-01-01 - initial version
 *
 * @since 2009-01-01
 */
public final class TestJToggleButton
    {
    // ------------------------------ FIELDS ------------------------------

    private static final Color LABEL_FOREGROUND = new Color( 0x0000b0 );

    // --------------------------- main() method ---------------------------

    /**
     * Debugging harness for a Frame
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        SwingUtilities.invokeLater( new Runnable()
        {
        /**
         * fire up a JFrame on the Swing thread
         */
        public void run()
            {
            final JFrame jFrame = new JFrame();
            final Container contentPane = jFrame.getContentPane();

            final ButtonGroup flowers = new ButtonGroup();

            final JToggleButton daffodil =
                    new JToggleButton( "daffodil", true );
            daffodil.setForeground( LABEL_FOREGROUND );
            // this modifies the unselected background colour only
            daffodil.setBackground( Color.YELLOW );
            daffodil.setFont( new Font( "Dialog", Font.BOLD, 15 ) );

            final JToggleButton impatiens =
                    new JToggleButton( "impatiens", false );
            final JToggleButton sunflower =
                    new JToggleButton( "sunflower", false );

            final ItemListener flowerListener = new ItemListener()
            {
            public void itemStateChanged( ItemEvent e )
                {
                // warning flowers.getSelection() returns a ButtonModel not the
                // JToggleButton.
                // Beware, you will get two events for each change, one to
                // remove a selection
                // and one to add a new one.
                System.out
                        .println( "daffodil:" + daffodil.isSelected() );
                System.out
                        .println( "impatiens:"
                                  + impatiens.isSelected() );
                System.out
                        .println( "sunflower:"
                                  + sunflower.isSelected() );
                /* ... */
                }
            };

            daffodil.addItemListener( flowerListener );
            impatiens.addItemListener( flowerListener );
            sunflower.addItemListener( flowerListener );
            // Note you don't attach the Listener to the ButtonGroup.

            // add JToggleButtons to ButtonGroup
            flowers.add( daffodil );
            flowers.add( impatiens );
            flowers.add( sunflower );

            // add JToggleButton to the JFrame
            contentPane.add( daffodil, BorderLayout.WEST );
            contentPane.add( impatiens, BorderLayout.CENTER );
            contentPane.add( sunflower, BorderLayout.EAST );

            jFrame.setSize( 300, 100 );
            jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            jFrame.validate();
            jFrame.setVisible( true );
            }
        } );
        }// end main
    }// end class
